/**
 * Created by AllanXu on 7/25/2016.
 */
define(function (require) {
	var Vue = require('vue'),
        Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities');

	var app = new Vue({
		el: '#map',
		data: {
			qrcode: '',
            userInfo: ''
		},
		methods:{
            init: function(){
                 //app.userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                var map;
                var gpsPoint;
                var baiduPoint;
                var gpsAddress;
                var baiduAddress;

                //根据IP获取城市
                //var myCity = new BMap.LocalCity();
                //myCity.get(getCityByIP);
                //navigator.geolocation.getCurrentPosition(app.mapIt,app.locationError);
                var geolocation = new BMap.Geolocation();
                geolocation.getCurrentPosition(app.mapIt);
                //app.mapIt();
                //获取GPS坐标
               // if (navigator.geolocation) {
                //    navigator.geolocation.getCurrentPosition(showMap, handleError, { enableHighAccuracy: true, maximumAge: 1000 });
                //} else {
                //    alert("您的浏览器不支持使用HTML 5来获取地理位置服务");
                //}
            },
            mapIt:function(r){
                var lon = r.point.lng;
                var lat = r.point.lat;
                //var lon="120.091637";
                //var lat="30.326828";
                var map = new BMap.Map("allmap");
                var point_dw = new BMap.Point(""+lon+"",""+lat+"");
                ///BMap.Convertor.translate(point_dw,0,translateCallback);
                map.centerAndZoom(point_dw,12);
                var gc = new BMap.Geocoder();
                //translateCallback = function (point_dw){
                    //var myIcon = new BMap.Icon("http://developer.baidu.com/map/jsdemo/img/fox.gif", new BMap.Size(300,150));
                    var myIcon_dw = new BMap.Icon("http://api.map.baidu.com/img/markers.png", new BMap.Size(23, 25), {
                        offset: new BMap.Size(10, 25), // 指定定位位置
                        imageOffset: new BMap.Size(0, 0 - 10 * 25) // 设置图片偏移
                    });
                    var marker_dw = new BMap.Marker(point_dw,{icon:myIcon_dw});
                    map.addOverlay(marker_dw);
                    map.setCenter(point_dw);
                //}
                //BMap.Convertor.translate(point_dw,2,translateCallback);

                _(Config.SERVICE.INDEX.ACTIVITYBASEDT,{
                    longitude:lon,
                    latitude:lat
                },function(data) {
                    console.log(data);
                    var point = new Array(); //存放标注点经纬信息的数组
                    var marker = new Array(); //存放标注点对象的数组
                    var info = new Array(); //存放提示信息窗口对象的数组
                    var searchInfoWindow =new Array();//存放检索信息窗口对象的数组

                    for (var i = 0; i < data.pageitems.length; i++) {
                        var mp = data.pageitems[i].map;
                        if (mp != '') {
                            var p0 = mp.split(',')[0].trim(); //
                            var p1 = mp.split(',')[1].trim(); //按照原数组的point格式将地图点坐标的经纬度分别提出来
                            point[i] = new window.BMap.Point(p0, p1); //循环生成新的地图点
                            var myIcon_sj = new BMap.Icon("http://api.map.baidu.com/img/markers.png", new BMap.Size(23, 25), {
                                offset: new BMap.Size(10, 25), // 指定定位位置
                                imageOffset: new BMap.Size(0, 0 - i * 25) // 设置图片偏移
                            });
                            marker[i] = new window.BMap.Marker(point[i],{icon:myIcon_sj}); //按照地图点坐标生成标记
                            map.addOverlay(marker[i]);
                            marker[i].setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画　　　　　　　　　　　　　　//显示marker的title，marker多的话可以注释掉
                            var label = new window.BMap.Label(data.pageitems[i].name, {offset: new window.BMap.Size(20, -10)});
                            marker[i].setLabel(label);

                            // 创建信息窗口对象
                            info[i] = "<p style=’font-size:12px;lineheight:1.8em;’><img src=" + data.pageitems[i].img + " style='float:right;zoom:1;overflow:hidden;width:100px;height:100px;margin-left:3px;'/>地址：" + data.pageitems[i].address + "</br> 电话：" + data.pageitems[i].phone + "</br>简介：" + data.pageitems[i].info + "</p>";
                            //创建百度样式检索信息窗口对象
                            searchInfoWindow[i] = new BMapLib.SearchInfoWindow(map, info[i], {
                                title: data.pageitems[i].name,      //标题
                                width: 290,             //宽度
                                height: 110,              //高度
                                panel: "panel",         //检索结果面板
                                enableSendToPhone: false,
                                enableAutoPan: true,     //自动平移
                                searchTypes: [
                                    BMAPLIB_TAB_SEARCH,   //周边检索
                                    BMAPLIB_TAB_TO_HERE,  //到这里去
                                    BMAPLIB_TAB_FROM_HERE //从这里出发
                                ]
                            });
                            //添加点击事件
                            marker[i].addEventListener("click",
                                (function (k) {
                                    // js 闭包
                                    return function () {
                                        //将被点击marker置为中心
                                        //map.centerAndZoom(point[k], 18);
                                        //在marker上打开检索信息窗口
                                        searchInfoWindow[k].open(marker[k]);
                                    }
                                })(i)
                            );
                        }
                    }
                });

                _(Config.SERVICE.INDEX.MEMBERDT,{
                    id:sessionStorage.id,
                    longitude:lon,
                    latitude:lat
                },function(data) {
                    console.log(data);

                    var points = new Array(); //存放标注点经纬信息的数组
                    var markers = new Array(); //存放标注点对象的数组
                    var infos = new Array(); //存放提示信息窗口对象的数组
                    var searchInfoWindows =new Array();//存放检索信息窗口对象的数组

                    for (var i = 0; i < data.obj.child.length; i++) {
                        var p0 = data.obj.child[i].longitude; //
                        var p1 = data.obj.child[i].latitude; //按照原数组的point格式将地图点坐标的经纬度分别提出来
                        points[i] = new window.BMap.Point(p0, p1); //循环生成新的地图点
                        var myIcon_hy = new BMap.Icon("img/1.png", new BMap.Size(25, 30), {
                            offset: new BMap.Size(0, 0), // 指定定位位置
                            imageOffset: new BMap.Size(0, 0) // 设置图片偏移
                        });
                        if(data.obj.child[i].sex=='女'){
                            myIcon_hy = new BMap.Icon("img/2.png", new BMap.Size(25, 30), {
                                offset: new BMap.Size(0, 0), // 指定定位位置
                                imageOffset: new BMap.Size(0, 0) // 设置图片偏移
                            });
                        }
                        markers[i] = new window.BMap.Marker(points[i],{icon:myIcon_hy}); //按照地图点坐标生成标记
                        map.addOverlay(markers[i]);
                        markers[i].setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画　　　　　　　　　　　　　　//显示marker的title，marker多的话可以注释掉
                        var imgurl='<img src="img/nan.png" width="10px;"/>';
                        if(data.obj.child[i].sex=='女')
                            imgurl='<img src="img/nv.png" width="10px;"/>';
                        var label = new window.BMap.Label(data.obj.child[i].nickName+imgurl, { offset: new window.BMap.Size(20, -10) });
                        markers[i].setLabel(label);

                        var monologue='';
                        var age='';
                        if (typeof(data.obj.child[i].monologue) != "undefined") {
                            monologue=data.obj.child[i].monologue;
                        }
                        if (typeof(data.obj.child[i].age) != "undefined") {
                            age=data.obj.child[i].age;
                        }

                        // 创建信息窗口对象
                        infos[i] = "<p style=’font-size:12px;lineheight:1.8em;’><a href='friend_detail.html?id="+data.obj.child[i].id+"'><img src="+data.obj.child[i].headImg+" style='float:right;zoom:1;overflow:hidden;width:100px;height:100px;margin-left:3px;'/></a>性别：" + data.obj.child[i].sex + "</br> 年龄：" + age + "</br>内心独白：" + monologue + "</br>最近登录时间：" + data.obj.child[i].time + "</p>";
                        //创建百度样式检索信息窗口对象
                        searchInfoWindows[i] = new BMapLib.SearchInfoWindow(map, infos[i], {
                            title  : data.obj.child[i].nickName,      //标题
                            width  : 290,             //宽度
                            height : 110,              //高度
                            panel  : "panel",         //检索结果面板
                            enableSendToPhone:false,
                            enableAutoPan : true,     //自动平移
                            searchTypes   :[
                                BMAPLIB_TAB_SEARCH,   //周边检索
                                BMAPLIB_TAB_TO_HERE,  //到这里去
                                BMAPLIB_TAB_FROM_HERE //从这里出发
                            ]
                        });
                        //添加点击事件
                        markers[i].addEventListener("click",
                            (function(k){
                                // js 闭包
                                return function(){
                                    //将被点击marker置为中心
                                    //map.centerAndZoom(point[k], 18);
                                    //在marker上打开检索信息窗口
                                    searchInfoWindows[k].open(markers[k]);
                                }
                            })(i)
                        );
                    }
                });
            },
            locationError:function(error){
                switch(error.code)
                {
                    case error.PERMISSION_DENIED:
                        alert("User denied the request for Geolocation.");
                        break;
                    case error.POSITION_UNAVAILABLE:
                        alert("Location information is unavailable.");
                        break;
                    case error.TIMEOUT:
                        alert("The request to get user location timed out.");
                        break;
                    case error.UNKNOWN_ERROR:
                        alert("An unknown error occurred.");
                        break;
                }
			}
		}
	});

    app.init();
});